package com.paas.form.core.model.js;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.field.FormDataModel;
import com.paas.form.core.model.ElementUI;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class VueData extends ElementUI {

    public VueData(int indentNum) {
        this.indentNum = indentNum;
    }

    public void s(StringBuilder str, FormDataModel formDataModel) {
        str.append(String.join("", Collections.nCopies(indentNum + 2, "\t"))).append(formDataModel.getFormModel()).append(": {\n");
        if (BeanUtil.isNotEmpty(formDataModel.getFields())) {
            JSONArray fiedldsJsarray= JSONArray.parseArray(formDataModel.getFields());
            List<FieLdsModel> formFields = new ArrayList<>();
            for (int i = 0; i <fiedldsJsarray.size() ; i++) {
                FieLdsModel fieldsModel = JSONObject.parseObject(String.valueOf(fiedldsJsarray.getJSONObject(i)), FieLdsModel.class);
                if(fiedldsJsarray.getJSONObject(i).get("vModel") != null){
                    fieldsModel.setVModel((String) fiedldsJsarray.getJSONObject(i).get("vModel"));
                }
                formFields.add(fieldsModel);
            }

            for (FieLdsModel fieLdsModel : formFields) {
                str.append(String.join("", Collections.nCopies(indentNum + 3, "\t"))).append(fieLdsModel.getVModel());
                if(BeanUtil.isNotEmpty(fieLdsModel.getConfig()) && StringUtil.isNotEmpty(fieLdsModel.getConfig().getDefaultValue())){
                    if("[]".equals(fieLdsModel.getConfig().getDefaultValue()) || "0".equals(fieLdsModel.getConfig().getDefaultValue()) ||fieLdsModel.getConfig().getDefaultValue().startsWith("[")  ){
                        str.append(": "+fieLdsModel.getConfig().getDefaultValue() +",\n");
                    }else if(StringUtil.isNotEmpty(fieLdsModel.getConfig().getDefaultValueType()) && fieLdsModel.getConfig().getDefaultValueType().equals("number")){
                        str.append(": "+fieLdsModel.getConfig().getDefaultValue() +",\n");
                    }else {
                        str.append(": \""+fieLdsModel.getConfig().getDefaultValue() +"\",\n");
                    }

                }else {
                    str.append(": undefined,\n");
                }
            }
        }
        str.append(String.join("", Collections.nCopies(indentNum + 2, "\t"))).append("},\n");
        str.append(String.join("", Collections.nCopies(indentNum + 2, "\t"))).append(formDataModel.getFormRules()).append(": {},\n");
    }

    @Override
    public void getElHtml(StringBuilder str) {

    }

    @Override
    public void getStartTagElHtml(StringBuilder str) {
        String dataTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "data() {";
        String returnTemplate = String.join("", Collections.nCopies(indentNum + 1, "\t")) + "return {";
        //
        String stringBuilder = dataTemplate + "\n" + returnTemplate;
        str.append(stringBuilder);
        str.append("\n");
        str.append(String.join("", Collections.nCopies(indentNum + 2, "\t")) + "showDialog: false,\n");
        str.append(String.join("", Collections.nCopies(indentNum + 2, "\t")) + "isdetail: false,\n");
        str.append(String.join("", Collections.nCopies(indentNum + 2, "\t")) + "addOrEdit: \"\",\n");

    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
        String dataTemplate = String.join("", Collections.nCopies(indentNum + 1, "\t")) + "}";
        String returnTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "},";
        //
        String stringBuilder = dataTemplate + "\n" + returnTemplate;
        str.append(stringBuilder);
        str.append("\n");
    }
}